let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

Page({

  data: {
    year: '',
    month: '',
    maxDate: '',
    gettingMyPayList: false,
    myPayList: []
  },

  onLoad: function (options) {
  
    that = this;

    let date = new Date(Date.now());

    that.setData({
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        maxDate: date.getFullYear() + '-' + (date.getMonth() + 1)
    });

    that.getMyPayList();

  },

  dateChange: function (e) {
      let date = e.detail.value.split('-');
      that.setData({
          year: parseInt(date[0]),
          month: parseInt(date[1])
      });
      that.getMyPayList();
  },

  goOrderDetail: function(e) {
    let orderId = e.currentTarget.dataset.orderid;
    wx.navigateTo({
        url: '/pages/orderDetail/orderDetail?orderId=' + orderId,
        fail: function() {
            wx.showModal({
                title: '跳转失败',
                content: '跳转到订单详情页面失败',
            });
        }
    });
  },

  getMyPayList: function() {
      let {year, month} = that.data;
      that.setData({ gettingMyPayList: true });
      wx.showNavigationBarLoading();
      wx.showToast({
          title: '查询中',
          icon: 'loading',
          duration: 10000,
          mask: true
      });
      wx.request({
          url: requestDomain + 'user/getMyPayList',
          method: 'POST',
          data: {
              storeId: util.globalData('storeId'),
              _3rdSession: util.globalData('_3rdSession'),
              year: year,
              month: month
          },
          success: function(result) {
            wx.hideNavigationBarLoading();
            wx.hideToast();
            that.setData({ gettingMyPayList: false });
            if(util.checkSuccess(result) && result.data && result.data.data) {
                result = result.data.data;
                let payModeList = {
                    wxPay: '微信支付',
                    cashPay: '现金支付'
                };
                let myPayList = [];
                let date, dateStr, orderPrice = 0;
                for (let orderIndex in result) {
                    let {orderId, payMode, goodsList, timestamp} = result[orderIndex];
                    date = new Date(timestamp * 1000);
                    dateStr = (date.getDate() > 9 ? date.getDate() : '0' + date.getDate()) + '日 ' + [date.getHours() > 9 ? date.getHours() : '0' + date.getHours(), date.getMinutes() > 9 ? date.getMinutes() : '0' + date.getMinutes(), date.getSeconds() > 9 ? date.getSeconds() : '0' + date.getSeconds()].join(':');
                    for (let goodsIndex in goodsList) {
                        let {foodPrice, goodsPrice} = goodsList[goodsIndex];
                        orderPrice += foodPrice || goodsPrice;
                    }
                    myPayList.push({
                        orderId: orderId,
                        orderMode: payModeList[payMode],
                        orderDate: dateStr,
                        orderPrice: orderPrice
                    });
                    orderPrice = 0;
                }
                
                that.setData({myPayList: myPayList});

                wx.stopPullDownRefresh();
            }
            else {
                wx.stopPullDownRefresh();
                util.showError('查询失败', '查询消费记录失败' + result.data.errMsg, result.data.errCode);
            }
          },
          fail: function() {
            wx.stopPullDownRefresh();
            wx.hideNavigationBarLoading();
            wx.hideToast();
            that.setData({ gettingMyPayList: false });
            util.showError('查询失败', '发起查询消费记录请求失败');
          }
      });
  },

  onPullDownRefresh: function() {
      that.getMyPayList();
  }

})